﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/15 1:25:38
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 主题管理类。
    /// </summary>
    public class Threads
    {
        private Threads() { }

        /// <summary>
        /// 保存主题。
        /// </summary>
        /// <param name="thread">主题实例对象。</param>
        /// <returns>返回创建主题状态。</returns>
        public static CreateThreadStatus SaveThread(Thread thread)
        {
            var status = CommonDataProvider.Current.SaveThread(thread);
            if(status == CreateThreadStatus.Created||
                status == CreateThreadStatus.Updated||
                status == CreateThreadStatus.Deleted)
                Caches.Remove(string.Format(cacheKey, thread.SettingsID, thread.UserID));
            return status;
        }

        /// <summary>
        /// 获取主题列表。
        /// </summary>
        /// <param name="pageIndex">页码。</param>
        /// <param name="pageSize">每页显示的记录数。</param>
        /// <param name="tagName">标签名称。</param>
        /// <param name="size">总记录数。</param>
        /// <returns>返回主题列表。</returns>
        public static List<Thread> GetTreadByTag(int pageIndex, int pageSize, string tagName, out int size)
        {
            return CommonDataProvider.Current.GetTreadByTag(pageIndex, pageSize, SiteSettings.Current.SettingsID, tagName, out size);
        }

        /// <summary>
        /// 获取主题列表。
        /// </summary>
        /// <param name="query">主题查询实例。</param>
        /// <param name="size">返回总记录数。</param>
        /// <returns>返回主题列表。</returns>
        public static List<Thread> GetThreads(ThreadQuery query, out int size)
        {
            return CommonDataProvider.Current.GetThreads(query, out size);
        }

        /// <summary>
        /// 更新标签。
        /// </summary>
        /// <param name="tag">标签实例。</param>
        /// <returns>返回执行结果。</returns>
        public static bool SaveTag(Tag tag)
        {
            if (CommonDataProvider.Current.SaveTag(tag))
            {
                Caches.Remove(string.Format(cacheKey, tag.SettingsID, tag.UserID));
                return true;
            }
            return false;
        }

        private static readonly string cacheKey = "Tags::SettingsID::{0}::UserID::{1}";

        /// <summary>
        /// 加载标签。
        /// </summary>
        /// <param name="userID">用户ID。</param>
        /// <returns>返回标签列表。</returns>
        public static List<Tag> LoadTags(int userID = -1)
        {
            if (userID == -1)
                userID = AppContext.Current.User.UserID;
            return Caches.Get<List<Tag>>(string.Format(cacheKey, SiteSettings.Current.SettingsID, userID), Globals.MinuteSeconds * 3, Globals.MinuteSeconds * 3, ()=>{
                return CommonDataProvider.Current.LoadTags(SiteSettings.Current.SettingsID, userID);
            });
        }

        /// <summary>
        /// 获取最新主题列表。
        /// </summary>
        /// <param name="categoryKey">分类ID。</param>
        /// <param name="size">加载数量。</param>
        /// <returns>返回新主题列表。</returns>
        public static List<Thread> GetThreads(string categoryKey, int size)
        {
            Category category = null;
            if(!string.IsNullOrEmpty(categoryKey))
                category = Categories.GetCategoryFromCache(categoryKey);
            return GetThreads(size, category == null ? null : (int?)category.CategoryID);
        }

        /// <summary>
        /// 获取最新主题列表。
        /// </summary>
        /// <param name="size">加载数量。</param>
        /// <param name="categoryID">分类ID。</param>
        /// <returns>返回新主题列表。</returns>
        public static List<Thread> GetThreads(int size = -1, int? categoryID = null)
        {
            if (size <= 0)
                size = Application.Current.Configuration.LastestThreadCount;
            string threadCacheKey = string.Format("LastestThreads::SettingsID.{0}::Categories.{1}::Sizes.{2}", SiteSettings.Current.SettingsID, categoryID, size);
            return Caches.Get<List<Thread>>(threadCacheKey, () =>
            {
                int allSize;
                var threads = GetThreads(new ThreadQuery { CategoryID = categoryID, IsApproved = true, PageIndex = 0, PageSize = size, OrderBy = "ThreadID", IsDesc = true }, out allSize);
                return Caches.Insert(threadCacheKey, threads, Globals.MinuteSeconds * 30);
            });
        }

        private static Thread GetThread(int threadID, string key, bool addViews) {
            return CommonDataProvider.Current.GetThread(threadID, key, addViews, SiteSettings.Current.SettingsID);
        }

        /// <summary>
        /// 获取主题实例。
        /// </summary>
        /// <param name="threadID">主题ID。</param>
        /// <param name="addViews">是否添加访问数量。</param>
        /// <returns>返回主题实例对象。</returns>
        public static Thread GetThread(int threadID, bool addViews = false)
        {
            return CommonDataProvider.Current.GetThread(threadID, null, addViews, SiteSettings.Current.SettingsID);
        }

        /// <summary>
        /// 获取主题实例。
        /// </summary>
        /// <param name="key">唯一键。</param>
        /// <param name="addViews">是否添加访问数量。</param>
        /// <returns>返回主题实例对象。</returns>
        public static Thread GetThread(string key, bool addViews = false)
        {
            return CommonDataProvider.Current.GetThread(-1, key, addViews, SiteSettings.Current.SettingsID);
        }

        /// <summary>
        /// 保存帖子。
        /// </summary>
        /// <param name="post">帖子实例。</param>
        /// <returns>返回执行结果。</returns>
        public static bool SavePost(Post post)
        {
            return CommonDataProvider.Current.SavePost(post);
        }

        /// <summary>
        /// 载入帖子。
        /// </summary>
        /// <param name="pageIndex">页码。</param>
        /// <param name="pageSize">每页显示记录数。</param>
        /// <param name="threadID">主题ID。</param>
        /// <param name="userid">用户ID。</param>
        /// <param name="size">总记录数。</param>
        /// <returns>返回帖子列表。</returns>
        public static List<Post> LoadPosts(int pageIndex, int pageSize, int? threadID, int? userid, out int size)
        {
            return CommonDataProvider.Current.LoadPosts(pageIndex, pageSize, threadID, SiteSettings.Current.SettingsID, userid, out size);
        }

        /// <summary>
        /// 删除帖子。
        /// </summary>
        /// <param name="postID">帖子ID。</param>
        /// <returns>返回删除结果。</returns>
        public static bool DeletePost(int postID)
        {
            return CommonDataProvider.Current.DeletePost(postID, SiteSettings.Current.SettingsID);
        }
    }
}